package com.apes.fn.tms.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能：支线线路
 *
 * @author Linwz
 * @create 2020-03-06 9:46
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BranchLine.CONST_TABLE_NAME)
public class BranchLine extends Model {
    public static final String CONST_TABLE_NAME = "tms_branch_line";

    @Id
    @Column(name = "id", length = 10)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "支线线路编码")
    @Comment("编码")
    private String id;

    @Name
    @Column(name = "name", length = 30)
    @Comment("线路名称")
    private String name;

//    @Column(name = "departure_station", length = 30)
//    @Comment("始发站")
//    private Site departureStation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "departure_station", referencedColumnName = "id", nullable = false)
    private Site departureStation;  //始发站

//    @Column(name = "terminal", length = 30)
//    @Comment("终点站")
//    private Site terminal;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "terminal", referencedColumnName = "id", nullable = false)
    private Site terminal;  //终点站

    @Column(name = "store_number", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("门店数")
    private int storeNumber = 0;

    @Column(name = "milage", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("里程数")
    private double milage = 0;

    @Column(name = "duration_total", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("线路总时长/运输耗时")
    private double durationTotal = 0;

    @Column(name = "line_type", nullable = false, length = 32)
    private String lineType; // 类型 MainLine:干线 ;BranchLine:支线


    @Column(name = "valid", columnDefinition = "number(2,0) default 1")
    @Comment("有效标记")
    private boolean valid = true;


    @OneToMany(mappedBy = "branchLine", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<BranchLineItem> branchLineItems = new ArrayList<>(); //支线线路明细信息

    public List<BranchLineItem> getBranchLineItems() {
        if (branchLineItems.isEmpty() || branchLineItems == null ) return null;
        for (BranchLineItem item : branchLineItems) {
            item.setBranchLine(this);
        }
        return branchLineItems;
    }


}
